// 1. 定义路由组件.
// 也可以从其他文件导入
import{createRouter,createWebHashHistory} from "vue-router"

import Login from "../components/Login.vue"
import Flavor from "../components/Flavor.vue"
import HomeC from "../components/HomeC.vue"
import FoodC from "../components/FoodC.vue"
import Recognize from "../components/Recognize.vue"
import FoodDetail from "../components/FoodDetail.vue"
import Checkout from "../components/Checkout.vue"
import Order from "../components/Order.vue"
import OrderDetail from "../components/OrderDetail.vue"
import Recommend from "../components/Recommend.vue"
import Account from "../components/Account.vue"
import HomeO from "../components/HomeO.vue"
import FoodO from "../components/FoodO.vue"

// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
const routes = [
  { path: '/', name:'login',component: Login},
  { path: '/customer/flavor', name:'flavor',component: Flavor },
  { path: '/customer/home', name:'homec',component: HomeC },
  { path: '/customer/food', name:'foodc',component: FoodC },
  { path: '/customer/checkout', name:'checkout',component: Checkout },
  { path: '/customer/recognize', name:'recognize',component: Recognize },
  { path: '/customer/order', name:'orderc',component: Order },
  { path: '/customer/fooddetail', name:'fooddetailc',component: FoodDetail},
  { path: '/customer/orderdetail', name:'orderdetailc',component: OrderDetail },
  { path: '/customer/recommend', name:'recommend',component: Recommend },
  { path: '/customer/account', name:'accountc',component: Account },
  { path: '/owner/home', name:'homeo',component: HomeO },
  { path: '/owner/food', name:'foodo',component: FoodO },
  { path: '/owner/fooddetail', name:'fooddetailo',component: FoodDetail },
  { path: '/owner/order', name:'ordero',component: Order },
  { path: '/owner/orderdetail', name:'orderdetailo',component: OrderDetail },
  { path: '/owner/account', name:'accounto',component: Account },

]

// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置，但我们在这里
// 暂时保持简单
const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history:createWebHashHistory(),
  routes, // `routes: routes` 的缩写
})

router.beforeEach((to,from,next)=>{
	let customerURL=["/customer/flavor",'/customer/home','/customer/food',"/customer/checkout",'/customer/order',
        '/customer/fooddetail','/customer/orderdetail','/customer/recommend','/customer/recognize', '/customer/account']
    let ownerURL=['/owner/home','/owner/food','/owner/fooddetail', '/owner/order', '/owner/orderdetail', '/owner/account']

    if(to.path==='/register'||to.path==='/'){
		next()
	}else{
        let account=localStorage.getItem('account')
        console.log("页面转换的account",account)
		if(account==""){
			alert("没有权限，请重新登录");
			return next('/');
		}else{
            account=JSON.parse(account)
            console.log(account.role)
            if(account.role=='customer'){
                if(customerURL.includes(to.path)){
                    console.log("请求的地址是",to.path)
                    next()
                }
                else{
                    return next('/customer/home');}}
                if(account.role=='owner'){
                    console.log("owner")
                    if(ownerURL.includes(to.path)){
                        next()
                    }
                    else{
                        return next('/owner/home');}}}
    }
})
export default router
// 现在，应用已经启动了！
